//
//  EMChatCustomBubbleView.m
//  CustomerSystem-ios
//
//  Created by dhc on 15/3/30.
//  Copyright (c) 2015年 easemob. All rights reserved.
//

#import "EMChatCustomBubbleView.h"
#import "EMChatTextBubbleView.h"

#define kImageWidth 60
#define kImageHeight 60
#define kTitleHeight 20

@implementation EMChatCustomBubbleView

@synthesize nameLabel = _nameLabel;
@synthesize cimageView = _cimageView;
@synthesize priceLabel = _priceLabel;

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
      //  self.backImageView.image = [UIImage imageNamed:BUBBLE_RIGHT_IMAGE_DINGDAN_NAME];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 15, 100, 0)];
        _titleLabel.textColor = RGBACOLOR(64, 64, 64,1);
        _titleLabel.font = [UIFont systemFontOfSize:13.0];
        _titleLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_titleLabel];
        
        _cimageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, CGRectGetMaxY(_titleLabel.frame) + 5, kImageWidth, kImageHeight)];
        _cimageView.image = [UIImage imageNamed:@"tabbar_chatsHL.png"];
        [self addSubview:_cimageView];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_cimageView.frame) + 5, CGRectGetMaxY(_titleLabel.frame) + 5, 120, 35)];
        _nameLabel.numberOfLines = 1;
        _nameLabel.textColor = RGBACOLOR(64, 64, 64,1);
        _nameLabel.font = [UIFont systemFontOfSize:13.0];
        _nameLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_nameLabel];
        
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_cimageView.frame) + 5, CGRectGetMaxY(_nameLabel.frame), 120, 15)];
        _priceLabel.font = [UIFont systemFontOfSize:15.0];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.textColor = [UIColor redColor];
        [self addSubview:_priceLabel];
        
        _label = [[UILabel alloc]init];
        _label.text = @"发送订单";
        _label.font = [UIFont systemFontOfSize:10.0];
        _label.textColor = RGBACOLOR(102, 102, 102,1);
        [self addSubview:_label];
    }
    return self;
}

-(CGSize)sizeThatFits:(CGSize)size
{
    CGFloat width = 3 * BUBBLE_VIEW_PADDING + kImageWidth + 120 + 60;
    CGFloat height = 2 * BUBBLE_VIEW_PADDING + kImageHeight + 20;
    
    NSDictionary *dic = [_model.message.ext objectForKey:@"msgtype"];
    if ([dic objectForKey:@"order"]) {
        height += kTitleHeight;
    }
    
    return CGSizeMake(width, height);
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSDictionary *dic = [_model.message.ext objectForKey:@"msgtype"];
    _cimageView.frame = CGRectMake(14, 17, 52, 52);
    _label.frame = CGRectMake(14, CGRectGetMaxY(_cimageView.frame)+10, 142, 13);
    if ([dic objectForKey:@"order"]) {
        _titleLabel.frame = CGRectMake(CGRectGetMaxX(_cimageView.frame) + 10, 12, self.frame.size.width - 20, kTitleHeight);
    }
    else{
    
        _titleLabel.frame = CGRectMake(CGRectGetMaxX(_cimageView.frame) + 10, 12, self.frame.size.width - 20, kTitleHeight);
    }
    _nameLabel.frame = CGRectMake(CGRectGetMaxX(_cimageView.frame) + 10, kTitleHeight , 150, 35);
    _priceLabel.frame = CGRectMake(CGRectGetMaxX(_cimageView.frame) + 10, CGRectGetMaxY(_nameLabel.frame)-2 , 150, 15);
}

#pragma mark - setter

- (void)setModel:(MessageModel *)model
{
    //    [super setModel:model];
    
    BOOL isReceiver = _model.isSender;
    NSString *imageName = isReceiver ? BUBBLE_LEFT_IMAGE_NAME : BUBBLE_RIGHT_IMAGE_DINGDAN_NAME;
    NSInteger leftCapWidth = isReceiver?BUBBLE_LEFT_LEFT_CAP_WIDTH:BUBBLE_RIGHT_LEFT_CAP_WIDTH;
    NSInteger topCapHeight =  isReceiver?BUBBLE_LEFT_TOP_CAP_HEIGHT:BUBBLE_RIGHT_TOP_CAP_HEIGHT;
    self.backImageView.image = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    
    NSDictionary *dic = [model.message.ext objectForKey:@"msgtype"];
    NSDictionary *itemDic = [dic objectForKey:@"order"] ? [dic objectForKey:@"order"] : [dic objectForKey:@"track"];
    _topLabel.text = [itemDic objectForKey:@"title"];
    _titleLabel.text = [itemDic objectForKey:@"order_title"];
    _nameLabel.text = [itemDic objectForKey:@"desc"];
    _priceLabel.text = [itemDic objectForKey:@"price"];
    
    NSLog(@"_topLabel-%@,_titleLabel-%@,_nameLabel-%@,_priceLabel-%@",[itemDic objectForKey:@"title"],[itemDic objectForKey:@"order_title"],[itemDic objectForKey:@"desc"],[itemDic objectForKey:@"price"]);
    
    //  NSString *imageName = [model.message.ext objectForKey:@"imageName"];
    //    if ([imageName length] > 0) {
    //        _cimageView.image = [UIImage imageNamed:imageName];
    //    }
    //    else{
    //        _cimageView.image = [UIImage imageNamed:@"imageDownloadFail.png"];
    //    }
    //[_cimageView sd_setImageWithURL:[itemDic objectForKey:@"img_url"]] placeholderImage:[UIImage imageNamed:@"default_head"]];
    [_cimageView load:[itemDic objectForKey:@"img_url"] placeholderImage:[UIImage imageNamed:@"default_head"]];
}

#pragma mark - public

-(void)bubbleViewPressed:(id)sender
{
    //    [self routerEventWithName:kRouterEventAudioBubbleTapEventName userInfo:@{KMESSAGEKEY:self.model}];
}


+(CGFloat)heightForBubbleWithObject:(MessageModel *)object
{
    NSDictionary *dic = [object.message.ext objectForKey:@"msgtype"];
    if ([dic objectForKey:@"order"]){
        return 2 * BUBBLE_VIEW_PADDING + kImageHeight + kTitleHeight ;
    }else{
        return 2 * BUBBLE_VIEW_PADDING + kImageHeight ;
    }
}



@end
